import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:encrypt/encrypt.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:share/share.dart';

class PreViewPage extends StatefulWidget {
  var data;

  PreViewPage(this.data);

  @override
  _PreViewPageState createState() => _PreViewPageState();
}

class _PreViewPageState extends State<PreViewPage> {
  File sharefile;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    savePic();
  }

  savePic() async {
    var dir = await getExternalStorageDirectory();
    log(dir.toString());
    File file =
        File("${dir.path}/${DateTime.now().microsecondsSinceEpoch}.jpg");
    if (!file.existsSync()) {
      file.createSync();
      file.writeAsBytesSync(widget.data);
    }
    sharefile = file;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff334455),
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        automaticallyImplyLeading: false,
        actions: [
          TextButton(
              onPressed: () async {
                Navigator.pop(context);
                Navigator.pop(context, sharefile);
              },
              child: Text("Done"))
        ],
      ),
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Column(
          children: [
            Container(
                constraints: BoxConstraints(
                  minHeight: 200,
                  maxWidth: 800
                ),
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.white, width: 3)),
                margin: EdgeInsets.symmetric(horizontal: 16),
                child: Image.memory(widget.data)),
            SizedBox(height: 10,),
            Container(
              width: double.infinity,
              margin: EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                children: [
                  // Expanded(
                  //     child: FlatButton.icon(
                  //   color: Color(0xffeeffee),
                  //   onPressed: () async {
                  //     String OCR_API_KEY = "230c9e7e6088957";
                  //     String OCR_API_URL = "https://api.ocr.space/Parse/Image";
                  //     FormData formdata = FormData.fromMap({
                  //       "apikey":OCR_API_KEY,
                  //       "base64Image":image
                  //     });
                  //   },
                  //   label: Text("ORC Translate"),
                  //   icon: Icon(Icons.enhance_photo_translate_rounded),
                  // )),
                  SizedBox(
                    width: 20,
                  ),
                  Expanded(
                      child: FlatButton.icon(
                    color: Color(0xffeeffee),
                    onPressed: () async {
                      Share.shareFiles([sharefile.path], text: "JOJO");
                    },
                    label: Text("Share Picture"),
                    icon: Icon(Icons.ios_share),
                  )),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
